package com.sort;

import java.util.Arrays;

/**
 * 冒泡排序
 * 作者：swk
 * 时间：
 * 公司：
 **/
public class BubbleSort {
    public static long count = 0;

    public static void main(String[] args) {
        int[] arr = new int[80000];
        for (int i = 0; i < 80000; i++) {
            arr[i] = (int) (Math.random() * 8000000);
        }
        long beginTime = System.currentTimeMillis();

        //System.out.println("排序前结果：" + Arrays.toString(arr));
        bubbleSort(arr);
        long endTime = System.currentTimeMillis();
        //System.out.println("排序后结果：" + Arrays.toString(arr));
        System.out.println("耗时：" + (endTime - beginTime));
        System.out.println("循环次数：" + count);
        count = 0;
    }

    public static void bubbleSort(int[] arr) {
        int temp = 0;
        boolean flag = false;

        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    flag = true;
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
                count++;
            }
            if (flag == false) {
                break;
            } else {
                flag = false;
            }
            // System.out.println("第" + (i + 1) + "轮排序结果：" + Arrays.toString(arr));
        }
        //System.out.println("最后结果：" + Arrays.toString(arr));
    }
}
